package com.woniuxy.mq02.controller;

import com.woniuxy.mq02.entity.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author author
 * @Create 2024-02-29 19:01
 */
@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/helloworld")
    public String sendhelloworld(){
        //使用helloword工作模式发送消息
        //String exchange, 交换机，helloword模式，交换机没有自定义使用默认，“”
        // String routingKey, 路由key，helloword模式，路由使用队列名
        // Object message,消息体，消息正文
        for (int i=1;i<=10;i++) {
            rabbitTemplate.convertAndSend("", "helloworld", "abc--"+i);
        }
        return  "helloworld-ok";
    }


    @RequestMapping("/all")
    public String all(){
        //使用helloworld发送邮件和短信
//        rabbitTemplate.convertAndSend("", "queueemail", "email-sms");
//        rabbitTemplate.convertAndSend("", "queuesms", "email-sms");

        //使用fanout模式发送邮件和短信:
        //String exchange, fanout类型的交换机
        // String routingKey, 空字符“”，和路由名无关，只要和当前交换机绑定的队列都会收到消息
        // final Object object，消息体
//        rabbitTemplate.convertAndSend("exchangefanout","","email-sms");


        //使用direct模式发送邮件和短信:
//        rabbitTemplate.convertAndSend("exchangedirect","email","email");
//        rabbitTemplate.convertAndSend("exchangedirect","sms","sms");


        //使用topic模式发送邮件和短信:
        rabbitTemplate.convertAndSend("exchangetopic","sms.email","sms-email");
        return  "all-ok";
    }
    @RequestMapping("/email")
    public String email(){
        //使用Helloword模式发送邮件
//        rabbitTemplate.convertAndSend("","queueemail","email-xxxx");

        //routing模式发送消息,交换机，
        // 路由：发送到那个队列，就使用绑定时指定的路由名
//        rabbitTemplate.convertAndSend("exchangedirect","email","email");


        //topics模式发送消息,交换机，
        rabbitTemplate.convertAndSend("exchangetopic","email",new User(1,"zs"));
        return  "email-ok";
    }
    @RequestMapping("/sms")
    public String sms(){
        //使用Helloword模式发送短信
//        rabbitTemplate.convertAndSend("","queuesms","email-xxxx");
        //routing模式发送消息,交换机，
        // 路由：发送到那个队列，就使用绑定时指定的路由名
//        rabbitTemplate.convertAndSend("exchangedirect","sms","sms");

        //topics模式发送消息,交换机，
        rabbitTemplate.convertAndSend("exchangetopic","sms","sms");
        return  "sms-ok";
    }
    @RequestMapping("/exchange")
    public String exchange(){
        rabbitTemplate.convertAndSend("exchangetopicxxxxx","sms","sms");
        return  "exchange-ok";
    }
    @RequestMapping("/queue")
    public String queue(){
        rabbitTemplate.convertAndSend("exchangetopic","smsxxxxx","sms");
        return  "queue-ok";
    }
}
